package com.wenxixuanwu.fruitstore.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wenxixuanwu.fruitstore.entity.Banner;
import com.wenxixuanwu.fruitstore.entity.Product;
import com.wenxixuanwu.fruitstore.entity.ProductVO;
import com.wenxixuanwu.fruitstore.mapper.BackProductMapper;
import com.wenxixuanwu.fruitstore.service.BackProductService;
import com.wenxixuanwu.fruitstore.service.exception.ProductNotFundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author :小凡凡
 * @version :1.0
 * @Project :fruitstore
 * @Date :2022/4/25
 */
@Service
public class BackProductServiceImpl implements BackProductService {

    @Autowired
    private BackProductMapper productMapper;

    @Override
    public List<Product> findProductAll() {
        List<Product> result = productMapper.findProductAll();
        if (result == null){
            throw new ProductNotFundException("商品信息未找到！");
        }
        return result;
    }

    @Override
    public Product findProductById(Integer id) {
        Product result = productMapper.findProductById(id);
        if (result == null){
            throw new ProductNotFundException("商品信息未找到！");
        }
        return result;
    }

    @Override
    public List<ProductVO> findProductVOAll() {
        List<ProductVO> result = productMapper.findProductVOAll();
        if (result == null){
            throw new ProductNotFundException("商品信息未找到");
        }
        return result;
    }

    @Override
    public ProductVO findProductVOById(Integer id) {
        return productMapper.findProductVOById(id);
    }

    @Override
    @Transactional
    public Integer insertProduct(Product product) {
        Product products = new Product();
        products.setUrl(product.getUrl());
        products.setName(product.getName());
        products.setCount(product.getCount());
        products.setSaleCount(0);
        products.setOldPrice(product.getOldPrice());
        products.setPrice(product.getOldPrice()*0.9);
        products.setDetail(product.getDetail());
        products.setCreated(new Date());
        products.setCategoryId(product.getCategoryId());
        products.setBrandId(1);
        products.setKindId(1);
        products.setOriginId(1);
        Integer result = productMapper.insertProduct(products);
        return result;
    }

    @Override
    @Transactional
    public Integer updateProduct(Product product) {
        Integer result = productMapper.updateProduct(product);
        if(result  < 1){
            throw new ProductNotFundException("商品信息更新失败");
        }
        return result;
    }

    @Override
    public Integer deleteProductById(Integer id) {
        Integer result = productMapper.deleteProductById(id);
        if (result != 1){
            throw new ProductNotFundException("商品信息删除失败，请联系管理员！");
        }
        return result;
    }

    //分页

    @Override
    public Page<Product> findByPageService(Integer pageCode, Integer pageSize) {
        //创建Page对象，传入两个参数；当前页和每页的显示记录数
        Page<Product> page = new Page<>(pageCode,pageSize);
        //2.将分页查询到的数据封装到Page对象中
        productMapper.selectPage(page,null);
        return page;
    }

    @Override
    public List<Product> downloadProduct(String year, String month) {
        return productMapper.downloadProduct(year, month);
    }
}
